////////////////////////////////////////////////////////////////////////////////
//                                                                            //
// The MIT License (MIT)                                                      //
//                                                                            //
// Copyright (c) 2013 Charles Teague                                          //
//                                                                            //
// Permission is hereby granted, free of charge, to any person obtaining a    //
// copy of this software and associated documentation files (the "Software"), //
// to deal in the Software without restriction, including without limitation  //
// the rights to use, copy, modify, merge, publish, distribute, sublicense,   //
// and/or sell copies of the Software, and to permit persons to whom the      //
// Software is furnished to do so, subject to the following conditions:       //
//                                                                            //
// The above copyright notice and this permission notice shall be included in //
// all copies or substantial portions of the Software.                        //
//                                                                            //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR //
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   //
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    //
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER //
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING    //
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER        //
// DEALINGS IN THE SOFTWARE.                                                  //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


using RPSDAL;
using RPSDAL.CollectionClasses;
using RPSDAL.EntityClasses;
using RPSDAL.HelperClasses;


using SD.LLBLGen.Pro.ORMSupportClasses;

public partial class MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Parameters.LoggedUser == -1)
            {
                Response.Redirect("Default.aspx");
            }
            LoadMenu();
        }
        string thisPage;
        thisPage = Page.GetType().Name;
        thisPage = thisPage.Substring(0, thisPage.Length - 5).ToLower();
        foreach (MenuItem item in Menu.Items)
        {

            if (item.NavigateUrl.Substring(0, item.NavigateUrl.Length - 5).ToLower() == thisPage)
            {
                item.Selected = true;
            }
            else
            {
                item.Selected = false;

                #region submenu 

                switch (thisPage) 
                {
                    case "contact":
                    case "markcalldone":
                    case "businessrule":
                    case "usercalldetails":
                    case "user":
                    case "assigncalls":
                        Menu.Items[0].Selected = true;
                        break;
                    case "contactgroup":
                    case "template":
                    case "role":
                        Menu.Items[1].Selected = true;
                        break;
                    case "contacttype":
                    case "callfinisheddetails":
                        Menu.Items[2].Selected = true;
                        break;
                    case "callfinishedpagefield":
                        Menu.Items[4].Selected = true;
                        break;
                    case "contactpagefield":
                        Menu.Items[5].Selected = true;
                        break;
                    
                        
                }

                #endregion submenu

            }
        }

        
    }
    private void BindSubMenu(string strText, string strUrl)
    {
        MenuItem omenu = new MenuItem(strText, null, null, strUrl);
        Menu.Items.Add(omenu);
        Menu.DataBind();
    }
    private void LoadMenu()
    {
        UserCollection cUsers = new UserCollection();
        IPredicateExpression filter = new PredicateExpression();
        filter.Add(UserFields.UserId == Parameters.LoggedUser);
        cUsers.GetMulti(filter);

        if (cUsers.Count == 1)
        {
            lblNameUser.Text = "User: " + cUsers[0].FirstName + " " + cUsers[0].LastName;
            RoleEntity eRole = new RoleEntity(cUsers[0].RoleId);
            HideLinks();
            ShowTopMenu();


            string PageName = "AssignedCalls";
            if (Session["PageName"] != null)
            {
                PageName = Session["PageName"].ToString();
            }

            switch (PageName)
            {
                #region administration

                case "AssignCalls":
                    // administration

                    bool showAdministration = false;

                    // hide assign calls
                    if (eRole.HasAssignCall)
                    {
                        //lnkAssignCalls.Style["display"] = "inline";
                        //lnkAssignCalls.Visible = true;
                        //lnkAssignCalls.Attributes.Add("OnClick", "window.location='UserCallsList.aspx'");
                        showAdministration = true;

                        BindSubMenu("Assign Calls", "UserCallsList.aspx");
                    }

                    // hide the maximum number of calls per week setup page
                    if (eRole.HasNumberCallsPerWeek)
                    {
                        //lnkMaxCalls.Style["display"] = "inline";
                        //lnkMaxCalls.Visible = true;
                        //lnkMaxCalls.Attributes.Add("OnClick", "window.location='NumberCallsWeek.aspx'");
                        showAdministration = true;

                        BindSubMenu("No. of Calls/Week", "NumberCallsWeek.aspx");
                    }


                    // hide set up of welcome template
                    if (eRole.HasWelcomeTemplate)
                    {
                        //lnkWelcomeTemplate.Style["display"] = "inline";
                        //lnkWelcomeTemplate.Visible = true;
                        //lnkWelcomeTemplate.Attributes.Add("OnClick", "window.location='WelcomeTemplate.aspx'");
                        showAdministration = true;

                        BindSubMenu("Welcome Template", "WelcomeTemplate.aspx");
                    }

                    // hide assign letters page
                    if (eRole.HasAssignLetters)
                    {
                        //lnkUserForLetters.Style["display"] = "inline";
                        //lnkUserForLetters.Visible = true;
                        //lnkUserForLetters.Attributes.Add("OnClick", "window.location='AssignUserResponsibleForLetters.aspx'");                      
                        showAdministration = true;

                        BindSubMenu("User Responsible For Letters", "AssignUserResponsibleForLetters.aspx");

                    }


                    // hide the administration of the call finished page
                    if (eRole.HasConfigureCallFinishedPage)
                    {
                        //lnkCallFinishedAdministration.Style["display"] = "inline";
                        //lnkCallFinishedAdministration.Visible = true;
                        //lnkCallFinishedAdministration.Attributes.Add("OnClick", "window.location='CallFinishedPageAdministration.aspx'");                      
                        showAdministration = true;

                        BindSubMenu("Configure Call Finished Page", "CallFinishedPageAdministration.aspx");
                    }

                    // hide the administration of the contact page
                    if (eRole.HasConfigureContactInterface)
                    {
                        //lnkContactAdministration.Style["display"] = "inline";
                        //lnkContactAdministration.Visible = true;
                        //lnkContactAdministration.Attributes.Add("OnClick", "window.location='ContactPageAdministration.aspx'");                      
                        showAdministration = true;

                        BindSubMenu("Configure Contact Interface", "ContactPageAdministration.aspx");

                    }

                    if (!showAdministration) 
                    {
                        mn_Administration.Style["display"] = "none";
                    }

                    break;

                #endregion administration

                #region users

                case "UserList":
                    //user

                    bool showUser = false;

                    // hide the user list
                    if (eRole.HasEditUser)
                    {
                        //lnkUserList.Style["display"] = "inline";
                        //lnkUserList.Visible = true;
                        //lnkUserList.Attributes.Add("OnClick", "window.location='UserList.aspx'");                      
                        showUser = true;

                        BindSubMenu("Users", "UserList.aspx");
                    }

                    // hide user roles
                    if (eRole.HasEditUserGroup)
                    {
                        //lnkRoles.Style["display"] = "inline";
                        //lnkRoles.Visible = true;
                        //lnkRoles.Attributes.Add("OnClick", "window.location='RoleList.aspx'");                      
                        showUser = true;

                        BindSubMenu("User Roles", "RoleList.aspx");
                    }


                    // hide user log
                    if (eRole.HasUserLog)
                    {
                        //lnkUserLog.Style["display"] = "inline";
                        //lnkUserLog.Visible = true;
                        //lnkUserLog.Attributes.Add("OnClick", "window.location='UserLog.aspx'");                      
                        showUser = true;

                        BindSubMenu("User Log", "UserLog.aspx");
                    }

                    if (!showUser) 
                    {
                        mn_users.Style["display"] = "none";
                    }

                    break;
                #endregion users

                #region summary report

                case "SummaryReport":
                    // summary report
                    // hide summary report
                    if (eRole.HasSummaryReport)
                    {
                        //lnkCallSummaryReport.Style["display"] = "inline";
                        //lnkCallSummaryReport.Visible = true;
                        //lnkCallSummaryReport.Attributes.Add("OnClick", "window.location='SummaryReport.aspx'");

                        BindSubMenu("Summary Report", "SummaryReport.aspx");
                    }
                    else
                    {
                        mn_summary.Style["display"] = "none";
                    }


                    break;

                #endregion users

                #region tasks

                case "AssignedCalls":
                case "AssignedLetterList":
                    //tasks
                    //lnkCalls.Style["display"] = "inline";
                    //lnkCalls.Visible = true;
                    //lnkCalls.Attributes.Add("OnClick", "window.location='AssignedCallList.aspx'");

                    BindSubMenu("Assigned Calls", "AssignedCallList.aspx");

                    //lnkLetters.Style["display"] = "inline";
                    //lnkLetters.Visible = true;
                    //lnkLetters.Attributes.Add("OnClick", "window.location='AssignedLetterList.aspx'");

                    BindSubMenu("Assigned Letters", "AssignedLetterList.aspx");

                    //lnkFinishedCalls.Style["display"] = "inline";
                    //lnkFinishedCalls.Visible = true;
                    //lnkFinishedCalls.Attributes.Add("OnClick", "window.location='CallFinishedList.aspx'");

                    BindSubMenu("Finished Calls", "CallFinishedList.aspx");

                    break;

                #endregion tasks

                #region business rules

                case "BusinessRuleList":
                    // business rules                    
                    // hide business rule list

                    bool showBusinessRules = false;

                    if (eRole.HasViewBusinessRules)
                    {
                        //lnkBusinessRuleList.Style["display"] = "inline";
                        //lnkBusinessRuleList.Visible = true;
                        //lnkBusinessRuleList.Attributes.Add("OnClick", "window.location='BusinessRuleList.aspx'");                      
                        showBusinessRules = true;

                        BindSubMenu("Business Rules", "BusinessRuleList.aspx");
                    }


                    // hide templates
                    if (eRole.HasViewTemplate)
                    {
                        //lnkTemplates.Style["display"] = "inline";
                        //lnkTemplates.Visible = true;
                        //lnkTemplates.Attributes.Add("OnClick", "window.location='TemplateList.aspx'");                      
                        showBusinessRules = true;

                        BindSubMenu("Templates", "TemplateList.aspx");
                    }

                    if (eRole.HasMailSetting) 
                    {
                        //lnkMailSettings.Visible = true;
                        //lnkMailSettings.Attributes.Add("OnClick", "window.location='MailSettings.aspx'");
                        showBusinessRules = true;

                        BindSubMenu("Mail Settings", "MailSettings.aspx");
                    }

                    if (!showBusinessRules) 
                    {
                        mn_rules.Style["display"] = "none";
                    }

                    break;

                #endregion business rules

                #region contacts

                case "ContactList":
                    // contacts

                    bool showContacts = false;

                    // hide contact list
                    if (eRole.HasViewContact)
                    {
                        //lnkContact.Style["display"] = "inline";
                        //lnkContact.Visible = true;
                        //lnkContact.Attributes.Add("OnClick", "window.location='ContactList.aspx'");                      
                        showContacts = true;

                        BindSubMenu("Contacts", "ContactList.aspx");
                    }

                    // hide contact groups
                    if (eRole.HasViewContactGroup)
                    {
                        //lnkContactGroups.Style["display"] = "inline";
                        //lnkContactGroups.Visible = true;
                        //lnkContactGroups.Attributes.Add("OnClick", "window.location='ContactGroupList.aspx'");                      
                        showContacts = true;

                        BindSubMenu("Contact Groups", "ContactGroupList.aspx");
                    }

                    // hide contact type administration
                    if (eRole.HasEditContactType)
                    {
                        //lnkContactTypeList.Style["display"] = "inline";
                        //lnkContactTypeList.Visible = true;
                        //lnkContactTypeList.Attributes.Add("OnClick", "window.location='ContactTypeList.aspx'");                      
                        showContacts = true;

                        BindSubMenu("Contact Types", "ContactTypeList.aspx");
                    }

                    if (!showContacts)
                    {
                        mn_contact.Style["display"] = "none";
                    }

                    break;

                #endregion contacts

            }


        }
    }

    private void ShowTopMenu()
    {
        UserCollection cUsers = new UserCollection();
        IPredicateExpression filter = new PredicateExpression();
        filter.Add(UserFields.UserId == Parameters.LoggedUser);
        cUsers.GetMulti(filter);

        if (cUsers.Count == 1)
        {
            RoleEntity eRole = new RoleEntity(cUsers[0].RoleId);


            #region administration


            // administration

            bool showAdministration = false;

            // hide assign calls
            if (eRole.HasAssignCall)
            {
                showAdministration = true;
            }

            // hide the maximum number of calls per week setup page
            if (eRole.HasNumberCallsPerWeek)
            {
                showAdministration = true;
            }

            // hide set up of welcome template
            if (eRole.HasWelcomeTemplate)
            {
                showAdministration = true;
            }

            // hide assign letters page
            if (eRole.HasAssignLetters)
            {
                showAdministration = true;
            }

            // hide the administration of the call finished page
            if (eRole.HasConfigureCallFinishedPage)
            {
                showAdministration = true;
            }

            // hide the administration of the contact page
            if (eRole.HasConfigureContactInterface)
            {
                showAdministration = true;
            }

            if (!showAdministration)
            {
                mn_Administration.Style["display"] = "none";
            }

            #endregion administration

            #region users

            //user

            bool showUser = false;

            // hide the user list
            if (eRole.HasEditUser)
            {
                showUser = true;
            }

            // hide user roles
            if (eRole.HasEditUserGroup)
            {
                showUser = true;
            }


            // hide user log
            if (eRole.HasUserLog)
            {
                showUser = true;
            }

            if (!showUser)
            {
                mn_users.Style["display"] = "none";
            }

            #endregion users

            #region summary report

            // summary report
            // hide summary report
            if (!eRole.HasSummaryReport)
            {
                mn_summary.Style["display"] = "none";
            }


            #endregion users

            #region business rules

            // business rules                    
            // hide business rule list

            bool showBusinessRules = false;

            if (eRole.HasViewBusinessRules)
            {
                showBusinessRules = true;
            }


            if (eRole.HasViewTemplate)
            {
                showBusinessRules = true;
            }

            if (!showBusinessRules)
            {
                mn_rules.Style["display"] = "none";
            }


            #endregion business rules

            #region contacts

            // contacts

            bool showContacts = false;

            if (eRole.HasViewContact)
            {
                showContacts = true;
            }

            // hide contact groups
            if (eRole.HasViewContactGroup)
            {
                showContacts = true;
            }

            // hide contact type administration
            if (eRole.HasEditContactType)
            {
                showContacts = true;
            }

            if (!showContacts)
            {
                mn_contact.Style["display"] = "none";
            }


            #endregion contacts
        }
    }

    #region administration redirect

    protected void ShowAdministrationMenu(object sender, ImageClickEventArgs e)
    {
        AdministrationRuleRedirect();
    }
    protected void hplAdministration_Click(object sender, EventArgs e)
    {
        AdministrationRuleRedirect();
    }

    private void AdministrationRuleRedirect()
    {
        Session["PageName"] = "AssignCalls";
        UserCollection cUsers = new UserCollection();
        IPredicateExpression filter = new PredicateExpression();
        filter.Add(UserFields.UserId == Parameters.LoggedUser);
        cUsers.GetMulti(filter);

        if (cUsers.Count == 1)
        {
            lblNameUser.Text = "User: " + cUsers[0].FirstName + " " + cUsers[0].LastName;
            RoleEntity eRole = new RoleEntity(cUsers[0].RoleId);

            // hide assign calls
            if (eRole.HasAssignCall)
            {
                Response.Redirect("UserCallsList.aspx");
            }

            // hide the maximum number of calls per week setup page
            if (eRole.HasNumberCallsPerWeek)
            {
                Response.Redirect("NumberCallsWeek.aspx");
            }


            // hide set up of welcome template
            if (eRole.HasWelcomeTemplate)
            {
                Response.Redirect("WelcomeTemplate.aspx");
            }

            // hide assign letters page
            if (eRole.HasAssignLetters)
            {
                Response.Redirect("AssignUserResponsibleForLetters.aspx");
            }


            // hide the administration of the call finished page
            if (eRole.HasConfigureCallFinishedPage)
            {
                Response.Redirect("CallFinishedPageAdministration.aspx");
            }

            // hide the administration of the contact page
            if (eRole.HasConfigureContactInterface)
            {
                Response.Redirect("ContactPageAdministration.aspx");
            }
        }
    }

    #endregion administration redirect

    #region business rules redirect

    protected void ShowBusinessRules(object sender, ImageClickEventArgs e)
    {
        BusinessRuleRedirect();
    }

    protected void hplBusinessRules_Click(object sender, EventArgs e)
    {
        BusinessRuleRedirect();
    }

    private void BusinessRuleRedirect()
    {
        Session["PageName"] = "BusinessRuleList";
        UserCollection cUsers = new UserCollection();
        IPredicateExpression filter = new PredicateExpression();
        filter.Add(UserFields.UserId == Parameters.LoggedUser);
        cUsers.GetMulti(filter);

        if (cUsers.Count == 1)
        {
            lblNameUser.Text = "User: " + cUsers[0].FirstName + " " + cUsers[0].LastName;
            RoleEntity eRole = new RoleEntity(cUsers[0].RoleId);

            // hide business rule list
            if (eRole.HasViewBusinessRules)
            {
                Response.Redirect("BusinessRuleList.aspx");
            }


            // hide templates
            if (eRole.HasViewTemplate)
            {
                Response.Redirect("TemplateList.aspx");
            }
        }
    }

    #endregion business rules redirect

    #region task redirect

    protected void ShowTasksMenu(object sender, ImageClickEventArgs e)
    {
        Session["PageName"] = "AssignedCalls";
        Response.Redirect("AssignedCallList.aspx");
    }

    protected void hplTasks_Click(object sender, EventArgs e)
    {
        Session["PageName"] = "AssignedCalls";
        Response.Redirect("AssignedCallList.aspx");
    }

    #endregion task redirect

    #region summary report redirect

    protected void ShowSummaryReport(object sender, ImageClickEventArgs e)
    {
        Session["PageName"] = "SummaryReport";
        Response.Redirect("SummaryReport.aspx");    
    }

    protected void lnkSummaryReport_Click(object sender, EventArgs e)
    {
        Session["PageName"] = "SummaryReport";
        Response.Redirect("SummaryReport.aspx");
    }

    #endregion summary report redirect

    #region users redirect

    protected void ShowUserMenu(object sender, ImageClickEventArgs e)
    {
        UsersRedirect(); 
    }


    protected void hplUsers_Click(object sender, EventArgs e)
    {
        UsersRedirect();
    }

    private void UsersRedirect()
    {
        Session["PageName"] = "UserList";


        UserCollection cUsers = new UserCollection();
        IPredicateExpression filter = new PredicateExpression();
        filter.Add(UserFields.UserId == Parameters.LoggedUser);
        cUsers.GetMulti(filter);

        if (cUsers.Count == 1)
        {
            lblNameUser.Text = "User: " + cUsers[0].FirstName + " " + cUsers[0].LastName;
            RoleEntity eRole = new RoleEntity(cUsers[0].RoleId);

            // hide the user list
            if (eRole.HasEditUser)
            {
                Response.Redirect("UserList.aspx");
            }

            // hide user roles
            if (eRole.HasEditUserGroup)
            {
                Response.Redirect("RoleList.aspx");
            }


            // hide user log
            if (eRole.HasUserLog)
            {
                Response.Redirect("UserLog.aspx");
            }
        }
    }

    #endregion users redirect

    #region contacts redirect

    protected void ShowContacts(object sender, ImageClickEventArgs e)
    {
        ContactsRedirect();
    }

    protected void hplContacts_Click(object sender, EventArgs e)
    {
        /*Session["PageName"] = "ContactList";
        LoadMenu();*/
        ContactsRedirect();
    }

    private void ContactsRedirect()
    {
        Session["PageName"] = "ContactList";
        //LoadMenu();    

        UserCollection cUsers = new UserCollection();
        IPredicateExpression filter = new PredicateExpression();
        filter.Add(UserFields.UserId == Parameters.LoggedUser);
        cUsers.GetMulti(filter);

        if (cUsers.Count == 1)
        {
            lblNameUser.Text = "User: " + cUsers[0].FirstName + " " + cUsers[0].LastName;
            RoleEntity eRole = new RoleEntity(cUsers[0].RoleId);

            // hide contact list
            if (eRole.HasViewContact)
            {
                Response.Redirect("ContactList.aspx");
            }

            // hide contact groups
            if (eRole.HasViewContactGroup)
            {
                Response.Redirect("ContactGroupList.aspx");
            }

            // hide contact type administration
            if (eRole.HasEditContactType)
            {
                Response.Redirect("ContactTypeList.aspx");
            }
        }
    }

    #endregion contacts redirect

    #region sign out

    protected void SignOut(object sender, ImageClickEventArgs e)
    {
        Parameters.LoggedUser = -1;
        Response.Redirect("Default.aspx");
    }
    protected void hplSignOut_Click(object sender, EventArgs e)
    {
        Parameters.LoggedUser = -1;
        Response.Redirect("Default.aspx");
    }

    #endregion sign out


    private void HideLinks()
    {
        /*lnkContact.Style["display"] = "none";
        lnkContactGroups.Style["display"] = "none";
        lnkContactTypeList.Style["display"] = "none";
        lnkContactAdministration.Style["display"] = "none";
        lnkAssignCalls.Style["display"] = "none";
        lnkBusinessRuleList.Style["display"] = "none";
        lnkCallFinishedAdministration.Style["display"] = "none";
        lnkCalls.Style["display"] = "none";
        lnkCallSummaryReport.Style["display"] = "none";
        lnkFinishedCalls.Style["display"] = "none";
        lnkLetters.Style["display"] = "none";
        lnkMaxCalls.Style["display"] = "none";
        lnkRoles.Style["display"] = "none";
        lnkTemplates.Style["display"] = "none";
        lnkUserForLetters.Style["display"] = "none";
        lnkUserList.Style["display"] = "none";
        lnkUserLog.Style["display"] = "none";
        lnkWelcomeTemplate.Style["display"] = "none";*/

        lnkContact.Visible = false;
        lnkContactGroups.Visible = false;
        lnkContactTypeList.Visible = false;
        lnkContactAdministration.Visible = false;
        lnkAssignCalls.Visible = false;
        lnkBusinessRuleList.Visible = false;
        lnkMailSettings.Visible = false;
        lnkCallFinishedAdministration.Visible = false;
        lnkCalls.Visible = false;
        lnkCallSummaryReport.Visible = false;
        lnkFinishedCalls.Visible = false;
        lnkLetters.Visible = false;
        lnkMaxCalls.Visible = false;
        lnkRoles.Visible = false;
        lnkTemplates.Visible = false;
        lnkUserForLetters.Visible = false;
        lnkUserList.Visible = false;
        lnkUserLog.Visible = false;
        lnkWelcomeTemplate.Visible = false;
    }





   
}
